/*
    nspire-gamekit - a simple homebrew game framework
    Copyright (C) 2011  Daniel Tang

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
    These routines assumes 32bit word sizes
    
    I tried minimizing branches as much as possible even though it scacrificed
    code redability
*/
#define SCREEN_WIDTH  #320
#define SCREEN_HEIGHT #240

.extern screenBufferWidth
.extern screenBufferHeight
.extern screenBufferData

//screenBufferAlphaMemCpy(void* dest, void* src, uint32_t size)
    
.global screenBufferAlphaMemCpy
screenBufferAlphaMemCpy:
    cmp r2, #0x0 //Bad things happen without this as I found out after 2 hours of debugging
    bxeq lr
    push { r4, lr }
    memcpyloop:
        
        ldrb r3, [r1]
        and r4, r3,   #0x000000F0
        cmp r4, #0
        strneb r3, [r0]
        
        add r0, r0, #1
        add r1, r1, #1
        sub r2, r2, #1
        
        cmp r2, #0
        bne memcpyloop
    pop { r4, pc }

//int gameCollisionCheckBuffers(void* buffer1, void* buffer2, uint32_t size)
.global gameCollisionCheckBuffers
gameCollisionCheckBuffers:
    cmp r2, #0x0
    bxeq lr
    
    push { r4, r5, lr }
    ldr r5, =0xF0F0F0F0

    collloop1:
        ldr r3, [r0]
        ldr r4, [r1]
        
        //Get alpha masks
        and r3, r3, r5
        and r4, r4, r5
        
        cmp r3, #0
        beq nocoll1
        
        //If this does not equal 0, theres a colision
        cmp r4, #0
        movne r0, #1
        popne { r4, r5, pc }
        
        nocoll1:
        add r0, r0, #4
        add r1, r1, #4
        sub r2, r2, #4
        cmp r2, #4
        bhs collloop1
    
    cmp r2, #0
        moveq r0, #0
        popeq { r4, r5, pc }
    
    collloop2:
        ldrb r3, [r0]
        ldrb r4, [r1]
        
        and r3, r3, #0x000000F0
        and r4, r4, #0x000000F0
        
        cmp r3, #0
        beq nocoll2
        
        //If this does not equal 0, theres a colision
        cmp r4, #0
        movne r0, #1
        popne { r4, r5, pc }
        
        nocoll2:
        add r0, r0, #1
        add r1, r1, #1
        sub r2, r2, #1
        cmp r2, #0
        bhi collloop2
    
    mov r0, #0
    pop { r4, r5, pc }
